package res;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;

@WebServlet(name = "FileServlet")
public class FileServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//  1．要获取下教文件的路径
        String  realPath =("C:\\Users\\Administrator\\Documents\\servltPro\\mvnPro\\target\\classes\\yazi.jpeg");
        System.out.println("下载文件的路径:"+realPath);
//  2．下教的文件名是啥?
        String fileName = realPath.substring(realPath.lastIndexOf("\\") + 1);
//  3、设置想办法让浏览器能够支持(Content-Disposition)下载我们需要的东西,中文文件名URLEncoder.encode编码，否则有可能乱码
        response.setHeader(  "Content-Disposition" , " attachment;filename="+ URLEncoder.encode(fileName,"UTF-8"));
//  4．获取下载文件的输入流
        FileInputStream in = new FileInputStream(realPath);// 5.创建缓冲区
        int len = 0;
        byte[] buffer = new byte[1024];//6.获取outputStream对象
        ServletOutputStream out = response.getOutputStream();
//7、将FiLeOutputStream流写入到buffer缓冲区,使开outputStream将缓冲区中的数据输出到客户端
        while ((len=in.read(buffer))>0){
            out.write(buffer,0,len);
        }

        in.close();
        out.close();
    }
}
